const fs = require('fs')
const {
  export_json_to_excel
} = require('./excel/Export2Excel')
// 记录使用导出功能的日志
module.exports = {
  updateExportLog(data) {
    const users = JSON.parse(fs.readFileSync('./data/user.json', {
      encoding: 'utf-8'
    }))
    if (Object.keys(users).includes(data.fingerprint)) {
      users[data.fingerprint].userName = data.userName
      users[data.fingerprint].exportLogs.unshift({
        time: new Date().toLocaleString()
      })
    } else {
      users[data.fingerprint] = {
        userName: data.userName,
        exportLogs: [{
          time: new Date().toLocaleString()
        }],
        checkMailLogs: [],
        sendMailLogs: []
      }
    }
    fs.writeFileSync('./data/user.json', JSON.stringify(users))
  },

  // 记录使用校验功能的日志
  updateCheckMailLog(data, status) {
    const users = JSON.parse(fs.readFileSync('./data/user.json', {
      encoding: 'utf-8'
    }))
    if (Object.keys(users).includes(data.fingerprint)) {
      users[data.fingerprint].userName = data.userName
      users[data.fingerprint].checkMailLogs.unshift({
        time: new Date().toLocaleString(),
        status
      })
    } else {
      users[data.fingerprint] = {
        userName: data.userName,
        exportLogs: [],
        checkMailLogs: [{
          time: new Date().toLocaleString(),
          status
        }],
        sendMailLogs: []
      }
    }
    fs.writeFileSync('./data/user.json', JSON.stringify(users))
  },

  // 记录使用发送邮件功能的日志
  updateSendMailLog(data, status) {
    const users = JSON.parse(fs.readFileSync('./data/user.json', {
      encoding: 'utf-8'
    }))
    if (Object.keys(users).includes(data.fingerprint)) {
      users[data.fingerprint].userName = data.userName
      users[data.fingerprint].sendMailLogs.unshift({
        time: new Date().toLocaleString(),
        status
      })
    } else {
      users[data.fingerprint] = {
        userName: data.userName,
        exportLogs: [],
        checkMailLogs: [],
        sendMailLogs: [{
          time: new Date().toLocaleString(),
          status
        }]
      }
    }
    fs.writeFileSync('./data/user.json', JSON.stringify(users))
  },

  // 导出Excel
  exportJson2Excel(theader, tbody) {
    export_json_to_excel({
      header: ['test1', 'test2'],
      data: [
        ['1', '2'],
        ['11', '22'],
      ],
      filename: 'test'
    })
    /* 
    表头
          const tHeader = [
            "客户姓名",
            "电话",
            "更新时间",
            "证件类型",
            "证件号码",
            "客户来源",
            "客户级别",
            "省、市、区/县",
            "详细地址",
            "创建人"
          ];

            const data = allData.data.map(v => {
              return [
                v.custName,
                v.custTel,
                v.custUpdateTime,
                v.custCardType,
                v.custCardNum,
                v.custOrigin,
                v.custLevel,
                v.custAddress,
                v.custDetailAddress,
                v.custCreatFounder
              ];
            });

              excel.export_json_to_excel({
                header: tHeader,
                data,
                filename:
                  "所有客户信息表" +
                  new Date(new Date().getTime() + 28800000)
                    .toJSON()
                    .substr(0, 19)
                    .replace("T", "_")
              });
     */
  }
}
